using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ChinaDynastyTours.Controls;
using DevExpress.XtraScheduler;
using System.Threading;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data.SqlClient;
using System.Configuration;

namespace ChinaDynastyTours.Views
{
    public partial class NotifyForm : DevExpress.XtraEditors.XtraForm
    {
        string _dbConnection = ConfigurationManager.ConnectionStrings[1].ConnectionString;
        DataSet _dataset;

        SqlDataAdapter _adapter;
        SqlConnection _conn;

        public NotifyForm()
        {
            InitializeComponent();
            this.schedulerControl.OptionsBehavior.RemindersFormDefaultAction = RemindersFormDefaultAction.SnoozeAll;
            this.schedulerControl.Start = DateTime.Now;
        }


        private void NotifyForm_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            _dataset = new DataSet();
            string selectAppointments = string.Format("SELECT * FROM sys_Scheduling where username='{0}'", Thread.CurrentPrincipal.Identity.Name);

            _conn = new SqlConnection(_dbConnection);
            _conn.Open();
            _adapter = new SqlDataAdapter(selectAppointments, _conn);
            _adapter.RowUpdated += new SqlRowUpdatedEventHandler(Adapter_RowUpdated);
            _adapter.Fill(_dataset, "sys_Scheduling");

            this.appointmentsBS.DataSource = _dataset;
            this.appointmentsBS.DataMember = "sys_Scheduling";
            this.appointmentsBS.Position = 0;

            this.schedulerStorage.Appointments.CustomFieldMappings.Add(new AppointmentCustomFieldMapping("UserName", "UserName"));
            this.schedulerStorage.Appointments.DataSource = appointmentsBS;
            this.schedulerStorage.Appointments.Mappings.AllDay = "AllDay";
            this.schedulerStorage.Appointments.Mappings.Description = "Description";
            this.schedulerStorage.Appointments.Mappings.End = "EndTime";
            this.schedulerStorage.Appointments.Mappings.Label = "Label";
            this.schedulerStorage.Appointments.Mappings.Location = "Location";
            this.schedulerStorage.Appointments.Mappings.RecurrenceInfo = "RecurrenceInfo";
            this.schedulerStorage.Appointments.Mappings.ReminderInfo = "ReminderInfo";
            this.schedulerStorage.Appointments.Mappings.Start = "StartTime";
            this.schedulerStorage.Appointments.Mappings.Status = "Status";
            this.schedulerStorage.Appointments.Mappings.Subject = "Subject";
            this.schedulerStorage.Appointments.Mappings.Type = "EventType";

            SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(_adapter);
            _adapter.InsertCommand = cmdBuilder.GetInsertCommand();
            _adapter.DeleteCommand = cmdBuilder.GetDeleteCommand();
            _adapter.UpdateCommand = cmdBuilder.GetUpdateCommand();
            _conn.Close();
        }

        void Adapter_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
        {
            if (e.Status == UpdateStatus.Continue && e.StatementType == StatementType.Insert)
            {
                int id = 0;
                using (SqlCommand cmd = new SqlCommand("SELECT IDENT_CURRENT('sys_Scheduling')", _conn))
                {
                    id = Convert.ToInt32(cmd.ExecuteScalar());
                }
                e.Row["ID"] = id;

            }
        }

        private void OnApptChangedInsertedDeleted(object sender, DevExpress.XtraScheduler.PersistentObjectsEventArgs e)
        {
            _adapter.Update(_dataset.Tables["sys_Scheduling"]);
            _dataset.AcceptChanges();
        }



        private void schedulerStorage_AppointmentChanging(object sender, PersistentObjectCancelEventArgs e)
        {
            Appointment apt = ((Appointment)e.Object);
            apt.CustomFields["UserName"] = Thread.CurrentPrincipal.Identity.Name;
        }

        private void barEditItem1_EditValueChanged(object sender, EventArgs e)
        {
            schedulerControl.GoToDate((DateTime)SelectDate.EditValue);
        }

        private void btnRefreshData_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
            
        }

        private void schedulerControl_EditAppointmentFormShowing(object sender, AppointmentFormEventArgs e)
        {
            MyAppointmentForm consoleF = new MyAppointmentForm(this.schedulerControl, e.Appointment);
            consoleF.LookAndFeel.ParentLookAndFeel = schedulerControl.LookAndFeel;
            e.DialogResult = consoleF.ShowDialog();
            e.Handled = true;
        }
    }
}